package ncku.account.gui;

import java.awt.*;

import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;


import javax.swing.*;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.SubTransactionList;
import ncku.account.event.Launcher;

//edit Transaction的介面
public class editTransaction extends JFrame {
	public JFrame editFrame;
	public int index;
	public newTransaction editPanel;
	
	//儲存新增Transaction的資料
    Date inputDate;
    String inputDescription;
    int inputAmount;
    Category inputCategory;
	
	public editTransaction(int selectIndex){
		this.index = selectIndex;  //存下transaction index
		
		editFrame = new JFrame("編輯交易");
		editFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
		editFrame.getContentPane().add (BorderLayout.CENTER,new editPanel(index));
		editFrame.pack();
		editFrame.setLocation(300, 150);
		editFrame.setSize(681, 500);
		editFrame.setVisible (true);
	}
	
	public void close(){
		this.editFrame.dispose();
	}
	
	
	class editPanel extends transactionModule{
		private int index;
		
		public editPanel(int indexSel){
			index = indexSel;
			
			JLTitle.setText("Edit Transaction");
			JDate.setDate(GlobalData.getTransaction(index).getDateinForm());  //設定Date
	        categorySelection.categorySelItem.setSelectedIndex(index);  //設定Category
			InputDescriptionBox.setText(GlobalData.getTransaction(index).getDescription()); //設定Description
			
			int amount = GlobalData.getTransaction(index).getAmount();
			if(amount>0){
				amountType.setSelectedIndex(0);
			}
			else{
				amountType.setSelectedIndex(1);
				amount = amount * (-1);
			}
			InputAmount.setText(String.valueOf(amount));  //設定Amount	
			subTransList.subTransactions = GlobalData.getTransaction(index).getAllSubTrans(); //取得該交易的SubTransList
			resetLayoutTable();  //重新畫上下方的subtransaction table
			
			JBSubmit.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) 
				{
					int selectIndex = categorySelection.categorySelItem.getSelectedIndex();
					if( (JDate.getDate() == null) || (InputDescriptionBox.getText().isEmpty()) || (InputAmount.getText() == "") || (selectIndex == -1)){  //沒有填寫完整資料
						JOptionPane.showMessageDialog(Launcher.frame, "資料尚未填寫完整！", "提示訊息", JOptionPane.ERROR_MESSAGE);
					}
					else{
						inputDate = JDate.getDate();  //讀取使用者輸入的日期
						inputDescription = InputDescriptionBox.getText();  //讀取使用者輸入的description
						try{
							inputAmount = Integer.parseInt(InputAmount.getText());  //讀取使用者輸入的amount
						}
						catch(Exception ex){
							JOptionPane.showMessageDialog(Launcher.frame, "金額須為大於零的整數！", "提示訊息", JOptionPane.ERROR_MESSAGE);
							return;
						}
						if(inputAmount<=0){
							JOptionPane.showMessageDialog(Launcher.frame, "金額須為大於零的整數！", "提示訊息", JOptionPane.ERROR_MESSAGE);
							return;
						}
						
						inputCategory = GlobalData.getCategory(selectIndex);
						int subTransSum = getSubTransSum();
						int amountTypeSel = amountType.getSelectedIndex();
						
						//Sub transaction definition
						ArrayList<SubTransaction> inputSubTrans = subTransList.getArrayListSubTrans();  //Sub transaction definition
						//inputSubTrans = GlobalData.checkSumOfAmount(inputAmount,editPanel.subTransList);
						
						if(inputSubTrans.size()!=0){
							if(subTransSum == inputAmount){
								inputAmount = checkAmount(amountTypeSel,inputAmount);
								inputSubTrans = checkSubTrans(amountTypeSel,inputSubTrans);
								//re calculate balance money
								//GlobalData.balanceMoney -= GlobalData.getTransaction(index).getAmount();
								
								GlobalData.edit(index, inputDate, inputDescription, inputAmount, inputCategory, inputSubTrans);
								//切換panel ，先把原本的移除掉，然後加入新的
			    				Launcher.frame.getContentPane().remove(1);
			    				Launcher.frame.getContentPane().add (BorderLayout.CENTER,new showTransaction());
			    				Launcher.frame.getContentPane().doLayout();			
								close();  //關閉該Frame
							}
							else{
								JOptionPane.showMessageDialog(Launcher.frame, "Transaction金額與Subtransaction總金額不符", "提示訊息", JOptionPane.ERROR_MESSAGE);
							}	
						}
						else{
							GlobalData.edit(index, inputDate, inputDescription, inputAmount, inputCategory, inputSubTrans);
							//切換panel ，先把原本的移除掉，然後加入新的
		    				Launcher.frame.getContentPane().remove(1);
		    				Launcher.frame.getContentPane().add (BorderLayout.CENTER,new showTransaction());
		    				Launcher.frame.getContentPane().doLayout();
							close();   //關閉該Frame
						}
					}	
				}});
			
			JBCancel.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) 
				{
					close();   //關閉該Frame
				}});
		}
	}
}